// pages/radio/index.js
const app = getApp()
var rightAnswer = []
Page({

  /**
   * 页面的初始数据
   */
  data: {
    index: 0,
    topic: [],
    isRadio: false,//是否多选
    isDati: false,//是否答题模式
    option: ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M'],
    id: '',
    right: '',
  },
  nextTopic() {
    if (this.data.index < this.data.topic.length - 1) {
      let key = `topic[${this.data.index}].isAnswer`
      this.setData({
        index: Number(this.data.index) + 1,
        [key]: (!this.data.isDati) && this.data.topic[this.data.index].myanswer && true
      })
    } else {
      wx.showToast({
        title: '已经是最后一个了',
        icon: 'none'
      })
    }
  },
  PrevTopic() {
    if (this.data.index > 0) {
      let key = `topic[${this.data.index}].isAnswer`
      this.setData({
        index: Number(this.data.index) - 1,
        [key]: (!this.data.isDati) && this.data.topic[this.data.index].myanswer && true
      })
    } else {
      wx.showToast({
        title: '已经是第一个了',
        icon: 'none'
      })
    }
  },
  choose(e) {
    let idx = e.currentTarget.id
    //控制是否可以选择
    if (this.data.topic[this.data.index].isAnswer) {
      return
    }
    let index = -1
    if (this.data.topic[this.data.index].myanswer) {
      index = this.data.topic[this.data.index].myanswer.indexOf(this.data.option[idx]);
    }
    let myanswer = this.data.topic[this.data.index].myanswer;
    if (!this.data.isRadio) {
      myanswer = ""
    }
    if ((!this.data.isRadio) || index == -1) {
      myanswer = myanswer + this.data.option[idx]
    } else {
      myanswer = myanswer.replace(this.data.option[idx], "");
    }
    let key = `topic[${this.data.index}].myanswer`
    let keyIsAnswer = `topic[${this.data.index}].isAnswer`
    this.setData({
      [key]: myanswer, //选中答案
      [keyIsAnswer]: this.data.isDati || this.data.isRadio ? false : true
    })
  },
  confirm() {
    rightAnswer = [];
    this.data.topic.forEach(item => {
      if (!item.myanswer) {
        return
      }
      let a = {
        'id': item.id,
        'uanswer': item.myanswer
      }
      rightAnswer.push(a)
    })
    console.log(rightAnswer)
    if (rightAnswer.length == this.data.topic.length) {
      let data = {
        method: 'paper.solution',
        paper_id: this.data.id,
        token: wx.getStorageSync('token'),
        user_answer: JSON.stringify(rightAnswer)
      }
      app.api.post('/api/index.html', data).then((res) => {
        if (res.status) {
          wx.showToast({
            title: res.msg,
            icon: "none"
          })
          setTimeout(() => {
            wx.navigateTo({
              url: '/pages/radioResult/index?id=' + res.data,
            })
          }, 500)
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none"
          })
          setTimeout(() => {
            wx.navigateBack({
              delta: 1
            })
          }, 500)
        }
      })
    } else {
      wx.showToast({
        title: '你还有未答的题',
        icon: "none"
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.type == 1) {
      this.setData({
        id: options.id,
        isRadio: false,//是否多选
        isDati: false,//是否答题模式
      })
      wx.setNavigationBarTitle({
        title: '单选题',
      })
    } else {
      this.setData({
        id: options.id,
        isRadio: true,//是否多选
        isDati: false,//是否答题模式
      })
      wx.setNavigationBarTitle({
        title: '多选题',
      })
    }
    this.getList()
  },
  getList() {
    let data = {
      method: 'paper.getquestion',
      paper_id: this.data.id,
      token: wx.getStorageSync('token')
    }
    app.api.post('/api/index.html', data).then((res) => {
      if (res.status) {
        let topicArr = [];
        res.data.list.forEach(item => {
          topicArr.push({ ...item, myanswer: '', isAnswer: false })
        })
        this.setData({
          topic: topicArr,
          right: res.data.paper_info,
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})